﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using mal.balades.Business.DataModel;
using mal.balades.Business.Log;
using mal.balades.Business.Manager;
using mal.balades.Business.Web;

namespace mal.balades.Web.Services
{
    [ServiceContract(Namespace = "mal.balades.Web.Services")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class VisitServices : ServiceBase
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public List<MapVisitList> GetClosedVisits(int visitId)
        {
            MalLogManager.WebLogger.InfoFormat("visitId:{0}", visitId);
            IEnumerable<VisitList> closedVisits = BaladesManager.GetClosedVisits(visitId);
            MalLogManager.WebLogger.InfoFormat("results:{0}", closedVisits.Count());

            List<MapVisitList> results = new List<MapVisitList>();

            foreach (VisitList visit in closedVisits)
            {
                results.Add(new MapVisitList(visit));
            }

            return results.OrderBy(v=>v.Distance).ToList();
        }

        [OperationContract]
        public int GetNumberOfVisits()
        {
            return BaladesManager.GetAllVisitsForMap().Count();
        }


        [OperationContract]
        public List<MapVisitList> GetVisits(int page, int rowPersPage)
        {            
            List<MapVisitList> results = BaladesManager.GetAllVisitsForMap().ToList();
            foreach (MapVisitList item in results)
            {
                item.Url = UrlManager.VisitUrl(item.Url);
                // item.Picture = GetImage.GetUrl(UrlManager.VisitPictureUrl(item.Picture)); 
                item.Picture = UrlManager.VisitPictureUrl(item.Picture);
                item.PlaceCategoryPicture = UrlManager.CategoryPictureUrl(item.PlaceCategoryPicture);
            }

            results = results.Skip(page * rowPersPage).Take(rowPersPage).ToList();

            return results;

        }

        
    }
}
